
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.workspace.WorkspaceBox
 *	
 *	@version 1.00 | Jan 15, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.workspace 
{
	import uk.co.lmfm.guppy.model.vo.WorkspaceVO;
	import uk.co.lmfm.guppy.view.components.events.WorkspaceBoxEvent;

	import com.bit101.components.Label;
	import com.bit101.components.Panel;
	import com.bit101.components.PushButton;

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * WorkspaceBox
	 */
	public class WorkspaceBox extends Sprite 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		public static const WIDTH : int = 120;
		public static const HEIGHT : int = 100;

		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _workspace : WorkspaceVO;
		private var _configured : Shape = new Shape();
		private var _cloneButton : PushButton = new PushButton();		private var _configureButton : PushButton = new PushButton();
		private var _background : Panel = new Panel();
		private var _nameLabel : Label = new Label();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function WorkspaceBox(__workspace : WorkspaceVO)
		{
			workspace = __workspace;
			
			initGraphics();
			configureListeners();
		}

		//	----------------------------------------------------------------
		//	PRIVATE METHODS
		//	----------------------------------------------------------------

		private function initGraphics() : void
		{
			_configured.graphics.beginFill(_workspace.isConfigured ? 0x45b95e : 0xedaf4d);
			_configured.graphics.drawRect(0, 0, WIDTH, 5);
			_configured.graphics.endFill();
			
			_configured.y = HEIGHT - _configured.height;
			
			_background.width = WIDTH;
			_background.height = HEIGHT;
			
			_nameLabel.x = 10;
			_nameLabel.y = 10;
			
			_cloneButton.x = 10;
			_cloneButton.y = _background.height - _cloneButton.height - 10;
			_cloneButton.width = _background.width - 20;
			_cloneButton.label = "Clone";
			
			_configureButton.x = 10;
			_configureButton.y = _cloneButton.y - _configureButton.height - 5;
			_configureButton.width = _background.width - 20;
			_configureButton.label = "Configure";
			
			addChild(_background);
			addChild(_configured);
			addChild(_cloneButton);			addChild(_configureButton);
			addChild(_nameLabel);
		}

		private function configureListeners() : void
		{
			_cloneButton.addEventListener(MouseEvent.CLICK, onButtonClicked);			_configureButton.addEventListener(MouseEvent.CLICK, onButtonClicked);
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonClicked(event : MouseEvent) : void
		{
			switch(event.currentTarget)
			{
				case _cloneButton : 
				
					dispatchEvent(new WorkspaceBoxEvent(WorkspaceBoxEvent.CLONE_BUTTON_PRESSED));
				
					break;
					
				case _configureButton :
				
					dispatchEvent(new WorkspaceBoxEvent(WorkspaceBoxEvent.CONFIGURE_BUTTON_PRESSED));
				
					break;
			}
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get workspace() : WorkspaceVO
		{
			return _workspace;
		}

		public function set workspace(value : WorkspaceVO) : void
		{
			_workspace = value;
			
			//_nameLabel.text = value.nativePath.match(/\b\w{2,}$/g)[0];
			_nameLabel.text = _workspace.name;
		}
	}
}
